package com.xxpt.user.feign;

import com.xxpt.common.annotation.NoAuth;
import com.xxpt.common.constant.SysConstant;
import com.xxpt.common.utils.BeanCopyUtils;
import com.xxpt.common.utils.Tools;
import com.xxpt.model.Result;
import com.xxpt.model.user.User;
import com.xxpt.user.dto.AddUserDTO;
import com.xxpt.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@NoAuth
@Slf4j
@RestController
public class AcountFeignController implements AccountFeign {
    @Autowired
    IUserService userService;

    @Override
    public Result<User> add(AddUserDTO userDTO) {
        Long now = System.currentTimeMillis();
        User user = BeanCopyUtils.copy(userDTO, User.class);
        user.setAddTime(now);
        user.setModifyTime(now);
        user.setSalt(Tools.get8UUID());
        user.setPassword(new SimpleHash(SysConstant.PASSWORD_ALOGORITHM, user.getPassword(),
                user.getSalt(), SysConstant.PASSWORD_ITERATIONS).toString());

        userService.save(user);
        return Result.ok(user);
    }

    @Override
    public User findById(Long id) {
        return userService.getById(id);
    }
}
